// Tierless
// Copyright © 2010 Alphora
// This file is licensed under a modified BSD-license (see tierless_license.txt).
using System;
using System.Runtime.Serialization;

using Tierless.Framework.Entities;
using Tierless.Framework.EntityTypes;

namespace Tierless.Framework.Constraints.Common
{
	/// <summary>
	/// Provides an abstract base class for all column-level constraints
	/// </summary>
	[DataContract]
	public abstract class ColumnConstraint : EntityConstraint
	{
		/// <summary>
		/// Creates a new ColumnConstraint.
		/// </summary>
		/// <param name="name">The name of the constraint.</param>
		/// <param name="entityTypeName">The name of the entity type on which the constraint is placed.</param>
		/// <param name="columnName">The name of the column on which the constraint is placed.</param>
		/// <param name="severity">The severity of the constraint.</param>
		/// <param name="message">The message to be displayed when the constraint is violated.</param>
		/// <param name="location">The locations in which it is valid to evaluate the constraint.</param>
		public ColumnConstraint(string name, string entityTypeName, string columnName, ViolationSeverity severity, string message, ConstraintLocation location)
			: base
			(
				name,
				entityTypeName,
				new[] { columnName },
				severity,
				message,
				location,
				ConstraintTransition.InsertUpdate
			)
		{
			_columnName = columnName;
		}

		/// <summary>
		/// Creates a new ColumnConstraint.
		/// </summary>
		/// <param name="name">The name of the constraint.</param>
		/// <param name="entityTypeName">The name of the entity type on which the constraint is placed.</param>
		/// <param name="columnName">The name of the column on which the constraint is placed.</param>
		/// <param name="severity">The severity of the constraint.</param>
		/// <param name="message">The message to be displayed when the constraint is violated.</param>
		public ColumnConstraint(string name, string entityTypeName, string columnName, ViolationSeverity severity, string message)
			: this
			(
				name,
				entityTypeName,
				columnName,
				severity,
				message,
				ConstraintLocation.Both
			)
		{}

		/// <summary>
		/// Creates a new ColumnConstraint.
		/// </summary>
		/// <param name="name">The name of the constraint.</param>
		/// <param name="entityTypeName">The name of the entity type on which the constraint is placed.</param>
		/// <param name="columnName">The name of the column on which the constraint is placed.</param>
		/// <param name="message">The message to be displayed when the constraint is violated.</param>
		public ColumnConstraint(string name, string entityTypeName, string columnName, string message)
			: this
			(
				name,
				entityTypeName,
				columnName,
				ViolationSeverity.Error,
				message,
				ConstraintLocation.Both
			)
		{}

		/// <summary>
		/// Creates a new ColumnConstraint.
		/// </summary>
		/// <param name="name">The name of the constraint.</param>
		/// <param name="entityTypeName">The name of the entity type on which the constraint is placed.</param>
		/// <param name="columnName">The name of the column on which the constraint is placed.</param>
		public ColumnConstraint(string name, string entityTypeName, string columnName)
			: this
			(
				name,
				entityTypeName,
				columnName,
				ViolationSeverity.Error,
				String.Format("Invalid value for {0}.", columnName),
				ConstraintLocation.Both
			)
		{}

		[DataMember]
		private string _columnName;

		/// <summary>
		/// Gets the name of the column on which the constraint is placed.
		/// </summary>
		public string ColumnName
		{
			get { return _columnName; }
		}
	}
}